package com.incross.dawin.http.parser;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import android.util.Log;

public class TrackingInfoParser {
	private String mStartTag, mQuarterTag, mHalfTag, mQuarter3Tag, mEndTag, mSkipTag, mChargeTag;
	
	private int mQuarterTime, mHalfTime, mQuarter3Time, mChargeTime;
	
	
	
	public TrackingInfoParser(NodeList list) {
		NodeList child = list.item(0).getChildNodes();
		
		for(int i = 0; i < child.getLength(); i++) {
			if(child.item(i).getNodeName().equals("Tracking")) {
				NamedNodeMap attributes = child.item(i).getAttributes();
				String value = child.item(i).getFirstChild().getNodeValue();
				
				AttributeObject attr = new AttributeObject();
				for(int j = 0; j < attributes.getLength(); j++) {
					String nodeValue = attributes.item(j).getNodeValue();
					String nodeName = attributes.item(j).getNodeName();
					if(nodeName.equals("rt")) {
						attr.setRtValue(nodeValue);
					} else if(nodeName.equals("event")) {
						attr.setEventValue(nodeValue);
					}
				}
				
				
				
				if(attr.getEventValue().equals("start")) {
					mStartTag = value;
				} else if(attr.getEventValue().equals("skip")) {
					mSkipTag = value;
				} else if(attr.getEventValue().equals("complete")) {
					mEndTag = value;
				} else if(attr.getEventValue().equals("firstQuartile")) {
					mQuarterTag = value;
					mQuarterTime = Integer.parseInt(attr.getRtValue()) * 1000;
				} else if(attr.getEventValue().equals("midpoint")) {
					mHalfTag = value;
					mHalfTime = Integer.parseInt(attr.getRtValue()) * 1000;
				} else if(attr.getEventValue().equals("thirdQuartile")) {
					mQuarter3Tag = value;
					mQuarter3Time = Integer.parseInt(attr.getRtValue()) * 1000;
				} else if(attr.getEventValue().equals("paytime")) {
					mChargeTag = value;
					mChargeTime = Integer.parseInt(attr.getRtValue()) * 1000;
				}
				
				
			}
		}
		
		
		/*BKLog.w"start tag : " + mStartTag);
		BKLog.w"1quarter tag : " + mQuarterTag + ", rt = " + mQuarterTime);
		BKLog.w"2quarter tag : " + mHalfTag + ", rt = " + mHalfTime);
		BKLog.w"3quarter tag : " + mQuarter3Tag + ", rt = " + mQuarter3Time);
		BKLog.w"finish tag : " + mEndTag);*/
	}
	
	
	
	public String getStartTag() {
		return mStartTag;
	}
	
	public String getQuarterTag(int quarter) {
		String result = null;
		switch(quarter) {
			case 1:
				result = mQuarterTag;
				break;
				
			case 2:
				result = mHalfTag;
				break;
				
			case 3:
				result = mQuarter3Tag;
				break;
		}
		
		return result;
	}
	
	public int getQuarterTime(int quarter) {
		int result = -1;
		switch(quarter) {
			case 1:
				result = mQuarterTime;
				break;
				
			case 2:
				result = mHalfTime;
				break;
				
			case 3:
				result = mQuarter3Time;
				break;
		}
		
		return result;
	}
	
	public String getEndTag() {
		return mEndTag;
	}
	
	public String getSkipTag() {
		return mSkipTag;
	}
	
	public String getChargeTag() {
		return mChargeTag;
	}
	
	public int getChargeTime() {
		return mChargeTime;
	}
	
	private class AttributeObject {
		private String rtValue;
		private String eventValue;
		
		
		public void setRtValue(String value) {
			rtValue = value;
		}
		
		public String getRtValue() {
			return rtValue;
		}
		
		public void setEventValue(String value) {
			eventValue = value;
		}
		
		public String getEventValue() {
			return eventValue;
		}
		
		
	}
}
